<template>
	<view class="mainpadding2">
		<view class="mainpadding_top ffffff radius flexbetween" @click="jumpurl('/pages/workbench/chidaob?type=2')">
			<view class="titletext bold hei_text white_nowrap">選擇計時套餐</view>
			<view class="flexright">
				<input type="text" class="hei_text main_size bold margin_right1" style="text-align: right;"
					placeholder="點擊選擇" v-model="cdbtc_msg.name" disabled="">
				<u-icon name="arrow-right" color="#999" size="16"></u-icon>
			</view>
		</view>
		<template v-if="ordermark_id==''">
			<view class="mainpadding_top ffffff radius flexbetween">
				<view class="titletext bold hei_text white_nowrap">客戶名稱</view>
				<input type="text" class="hei_text main_size bold margin_right1" style="text-align: right;"
					placeholder="請輸入" v-model="username">
			</view>
			<view class="mainpadding_top ffffff radius flexbetween">
				<view class="titletext bold hei_text white_nowrap">客戶電話</view>
				<input type="number" class="hei_text main_size bold margin_right1" style="text-align: right;"
					placeholder="請輸入" v-model="phone">
			</view>
		</template>
		<view class="mainpadding_top ffffff radius" v-if="cdbtc_id">
			<view class="flexbetween pdl" :class="index==0?'':'margin_top'"
				v-for="(item,index) in cdbtc_msg.cdbjhsf_data_text" :key="item.id">
				<view class="titletext bold hei_text white_nowrap">{{item.cdbjhsf.name}}人數<text v-if="cdbtc_id"
						class="xiaohuang margin-left">${{item.price}}</text></view>
				<view class="flexbetween bghui radius" style="padding: 10rpx;">
					<view class="reduceimg" @click="reduceFn(item)"></view>
					<input type="number" style="width: 100rpx; text-align: center;" v-model="item.num">
					<view class="addimg" @click="addFn(item)"></view>
				</view>
			</view>
		</view>
		<view class="mainpadding posibottom" style="bottom: 0;">
			<view class="flexcenter" v-if="cdbtc_id">
				<view class="titletext bold hei_text white_nowrap">合計：<text
						class="xiaohuang margin-left">{{allPrice}}</text></view>
			</view>
			<view class="submitbtn margin_top" @click="submit">確定</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id: "", //桌臺使用id
				ordermark_id: "", //預約訂單id
				shop_id: uni.getStorageSync("shop_id"),
				cdbtc_id: "",
				cdbtc_msg: {},
				username: "", //客戶姓名
				phone: "", //客戶電話
				issubmit:false
			}
		},
		onLoad(options) {
			if (options.id) {
				this.id = options.id //桌台id
				if (options.ordermark_id) {
					this.ordermark_id = options.ordermark_id //预约订單id
				}
			}
			uni.$on("checkitemcdb", data => {
				this.cdbtc_id = data.id
				data.cdbjhsf_data_text.forEach(item => {
					this.$set(item, "num", 0)
				})
				this.cdbtc_msg = data
			})
		},
		onUnload() {
			uni.$off("checkitemcdb")
		},
		computed: {
			allPrice() {
				let price = 0
				if (this.cdbtc_id) {
					this.cdbtc_msg.cdbjhsf_data_text.forEach(item => {
						price = item.price * item.num + price
					})
				}
				return price
			}
		},
		methods: {
			reduceFn(item) { //減
				if (item.num == 0) {
					return false
				}
				item.num = item.num - 1
			},
			addFn(item) { //加
				item.num = item.num + 1
			},
			submit() { //确定
				let num = 0
				let cdbjhsf_data = []
				this.cdbtc_msg.cdbjhsf_data_text.forEach(item => {
					cdbjhsf_data.push({
						name: item.cdbjhsf.name,
						num: item.num,
						price: item.price,
						total_price: item.num * item.price
					})
					num = item.num + num
				})

				if (this.ordermark_id) { //用戶下單商家給安排入座走這裏
					if (num == 0) {
						this.http.toast("請至少添加一位客戶")
						return false
					}
					if(this.issubmit) return
					this.issubmit = true
					this.http.request("/api/ordermake/addOrdermake", 'POST', {
						order_type: 3, //订單类型:1=正常,2=预约订單,3=吃得饱订單
						cdbjhsf_data,
						pay_price: this.allPrice,
						cdbtc_id: this.cdbtc_id,
						table_id: this.id,
						shop_id: uni.getStorageSync("shop_id"),
						ordermark_id: this.ordermark_id, //订單id
					}).then(res => {
						if (res.code == 1) {
							this.http.toast(res.msg)
							setTimeout(()=>{
								uni.switchTab({
									url: "/pages/index/index"
								})
							},500)
						}else{
							this.issubmit = false
							this.http.toast(res.msg)
						}
					})
				} else { //商家直接給用戶下單并且安排入座走這裏
					if (this.username == '') {
						this.http.toast("請輸入客戶名稱")
						return false
					}
					if (this.phone == '') {
						this.http.toast("請輸入客戶電話")
						return false
					}
					if (num == 0) {
						this.http.toast("請至少添加一位客戶")
						return false
					}
					if(this.issubmit) return
					this.issubmit = true
					this.http.request("/api/ordermake/directOrdermake", 'POST', {
						order_type: 3, //订單类型:1=正常,2=预约订單,3=吃得饱订單
						cdbjhsf_data,
						pay_price: this.allPrice,
						cdbtc_id: this.cdbtc_id,
						table_id: this.id,
						shop_id: uni.getStorageSync("shop_id"),
						username: this.username, //客戶姓名
						phone: this.phone, //客戶電話
					}).then(res => {
						if (res.code == 1) {
							this.http.toast(res.msg)
							setTimeout(()=>{
								uni.switchTab({
									url: "/pages/index/index"
								})
							},500)
						}else{
							this.issubmit = false
							this.http.toast(res.msg)
						}
					})
				}
			},
		}
	}
</script>

<style lang="scss" scoped>

</style>